package com.hevs.samplewebapp.client.screens;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.hevs.samplewebapp.client.services.UserAdministrationServiceImpl;
import com.hevs.samplewebapp.shared.security.CredentialLevel;

/**
 * Dialogbox to add a new user
 * <p>It provide methods for adding a new user.</p>
 * 
 * @author Thomas Rouvinez
 * @Date: 16.04.2012
 *
 */

public class AddNewUserWidget {

	// ------------------------------------------------------
    // Constructor.
    // ------------------------------------------------------
	
	/**
	 * Constructor of the DialogBox, creates all necessary UI Objects
	 * @param userService
	 * Reference of the AdministrationService, it is used to call the add user method
	 * @see UserAdministrationServiceImpl
	 * @param manageUsers
	 * Reference of the ManageUserWidget, it is used to refresh the Widget, after adding a new user
	 * @see ManageUsersWidget
	 */
	public AddNewUserWidget(final UserAdministrationServiceImpl userService, final ManageUsersWidget manageUsers) {
		/**
		 * DialogBox to show all Textboxes and Buttons
		 */
		final DialogBox box = new DialogBox();
		// 
		box.setGlassEnabled(true);
		box.setAnimationEnabled(true);
		box.setTitle("Create a new user");

		// Create a table to layout the content.
		VerticalPanel dialogContents = new VerticalPanel();
		HorizontalPanel hPanel = new HorizontalPanel();
		
		// creating necessary UI Objects
		Button create = new Button("Create");
		Button cancel = new Button("Cancel");
		final TextBox firstName = new TextBox();
		final TextBox lastName = new TextBox();
		final TextBox login = new TextBox();
		final TextBox email = new TextBox();
		final ListBox credentialLevel = new ListBox();

		// Define types of authorization level.
		for(int i=0;i<CredentialLevel.getSize();i++)
			credentialLevel.addItem(CredentialLevel.get(i).getName());

		FlexTable input = new FlexTable();

		input.setHTML(1, 0, "First name:");
		input.setWidget(1, 1, firstName);
		input.setHTML(2, 0, "Last name:");
		input.setWidget(2, 1, lastName);
		input.setHTML(3, 0, "Login:");
		input.setWidget(3, 1, login);
		input.setHTML(4, 0, "Email:");
		input.setWidget(4, 1, email);
		input.setHTML(5, 0, "Credential Level:");
		input.setWidget(5, 1, credentialLevel);

		cancel.setWidth("100%");
		create.setWidth("100%");

		// ------------------------------------------------------
        // Listeners.
        // ------------------------------------------------------
		create.addClickHandler(new ClickHandler() {	
			@Override
			public void onClick(ClickEvent event) {
				// Send the newly created user.
				JSONObject user = new JSONObject();

				try {
					user.put("firstName", new JSONString(firstName.getText()));
					user.put("lastName", new JSONString(lastName.getText()));
					user.put("login", new JSONString(login.getText()));
					user.put("email", new JSONString(email.getText()));
					user.put("credentialLevel", new JSONString("" + credentialLevel.getSelectedIndex()));

					userService.create(user);
					
					manageUsers.getData();

					box.hide();
				} catch (JSONException e) {
					Window.alert("Failed to create new user");
				}
			}
		});

		cancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				//clear the fields
				firstName.setText("");
				lastName.setText("");
				login.setText("");
				email.setText("");
				box.hide();
			}
		});

		hPanel.add(create);
		hPanel.add(cancel);

		hPanel.setWidth("100%");

		dialogContents.add(input);
		dialogContents.add(hPanel);
		box.setWidget(dialogContents);

		box.show();
		box.center();
	}
}